package com.orifine.sql.chain;

import com.orifine.orm.model.ColumnModel;
import com.orifine.sql.SyntaxString;
import com.orifine.utils.ClassUtils;

public class ChainAliasColumn implements ChainSql {
	private String name;
	private String alias;
	
	public ChainAliasColumn(ChainSelectColumn column, String alias){
		this.name = column.sql().toString();
		this.alias = alias;
	}
	
	public ChainAliasColumn(ChainSelectColumn column){
		this.name = column.sql().toString();
	}
	
	public ChainAliasColumn(ColumnModel column, String alias){
		this.name = ClassUtils.getLowerName(column.getTable().getName()) + "." + ClassUtils.getLowerName(column.getName());
		this.alias = "'"+alias+"'";
	}
	
	public ChainAliasColumn(ColumnModel column){
		this.name = ClassUtils.getLowerName(column.getTable().getName()) + "." + ClassUtils.getLowerName(column.getName());
	}

	@Override
	public void chain(SyntaxString out) {
		out.addWord(name);
		if(alias != null){
			out.addWord("AS").addWord(alias);
		}
	}

	@Override
	public String sql() {
		SyntaxString syntaxString = new SyntaxString();
		chain(syntaxString);
		return syntaxString.toString();
	}
}
